package com.tourlaid.mamagerment.ao.wx.impl

import com.tourlaid.mamagerment.ao.wx.PublishThoughtAo
import com.tourlaid.mamagerment.common.Contant
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.service.StoreService
import com.tourlaid.mamagerment.service.ThoughtService
import com.tourlaid.mamagerment.service.UserService
import com.tourlaid.mamagerment.util.BosUtil
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import org.springframework.util.StringUtils
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/9/23.
 */
@Service("publishThoughtAo")
class PublishThoughtAoImpl : PublishThoughtAo {

    @Resource
    val thoughtService: ThoughtService? = null
    @Resource
    val userService: UserService? = null
    @Resource
    val storeService: StoreService? = null

    override fun publish(thought: String, base64Datas: String) {
        if (StringUtils.isEmpty(thought)) {
            throw TldException (TldExceptionEnum.THOUGHT_NULL)
        }
        val dataList: List<String> = base64Datas.split("##")
        val fileNameList = BosUtil.PutObject (Contant.BUCKET_THOUGHT, dataList)
        if (CollectionUtils.isEmpty(fileNameList)) {
            throw TldException (TldExceptionEnum.THOUGHT_IMG_NULL)
        }
        thoughtService!!.insertThought(thought, fileNameList)
        //发表的用户增加积分
        userService!!.plusIntegral(LoginUtil.userId, 10)
        //店铺增加说说数
        storeService!!.plusWhenThoughtIns(5, 1)
    }
}
